from direct.gui.DirectGui import OnscreenText
from pandac.PandaModules import TextNode

# set button text
def BtnTextSet(btn, text, text_scale = None):
    btn['text'] = (text,text,text,text)
    if text_scale:
        btn["text_scale"] = text_scale

def BtnCmdSet(btn, cmd, extraArgs = None):
    btn["command"] = cmd
    if extraArgs: btn["extraArgs"] = extraArgs

# get scale tuple-3 from frame
def ScaleGet(frame):
    scale = frame['scale']
    if not scale:
        scale = (1,1,1)
    elif isinstance(scale,int) or isinstance(scale,float):
        scale = (scale,1,scale)
    return scale

def HeightGet(frame):
    scale = ScaleGet(frame)
    frameSize = frame["frameSize"]
    ret = (frameSize[3] - frameSize[2])*scale[0] if frameSize else 0
    return ret

def WidthGet(frame):
    scale = ScaleGet(frame)
    frameSize = frame["frameSize"]
    ret = (frameSize[1] - frameSize[0])*scale[2] if frameSize else 0
    return ret

# set alignment of text in DirectFrame
def AlignText(frame,align = "left"):
    # align = "left", "left,top", "left,bottom",
    #         "right", "right,top", "right,bottom",
    #         "top","bottom"
    #         ""
    if ScaleGet(frame) != (1,1,1):
        print "AlignText only can handle scale = (1,1,1)"
        return

    align = align.split(",")
    bounds = frame["frameSize"]
    if bounds:
        boundLeft,boundRight,boundBottom,boundTop = bounds
    else: return

    for nodeName in frame.components():
        guiObject = frame.component(nodeName)
        if not isinstance(guiObject, OnscreenText):
            continue # skip guiObject if not a OnscreenText

        if "left" in align:
            guiObject['align'] = TextNode.ALeft
            x = boundLeft
        elif "right" in align:
            guiObject['align'] = TextNode.ARight
            x = boundRight
        else:
            guiObject['align'] = TextNode.ACenter
            x = (boundLeft+boundRight)*0.5

        textHeight = guiObject['scale'][1]
        if "top" in align:
            y = boundTop - (textHeight*0.75)
        elif "bottom" in align:
            y = boundBottom + (textHeight*0.25)
        else:
            # move text a little down
            #print frame, textHeight
            y = (boundBottom+boundTop)*0.5 - (textHeight*0.25)

        guiObject['pos']=(x,y)

        # test - draw a frame around text object
        #guiObject['frame']=(1,1,1,1)


# set properties of indicator in DirectRadioButton
def AlignIndicator(radioButton, scale, align = "left"):
    if isinstance(scale, tuple): scale = min(scale)

    indicatorLabel = radioButton.component('indicator')
    half_scale = scale*0.5

    # set pos of indicatorLabel
    bounds = radioButton["frameSize"]
    if bounds:
        boundLeft,boundRight,boundBottom,boundTop = bounds
        align = align.split(",")
        if "left" in align:
            x = boundLeft+half_scale
        elif "right" in align:
            x = boundRight-half_scale
        else:
            x = (boundLeft+boundRight)*0.5
        if "top" in align:
            z = boundTop-half_scale
        elif "bottom" in align:
            z = boundBottom+half_scale
        else:
            z = (boundTop+boundBottom)*0.5
        indicatorLabel.setPos(x,0,z)

    # set scale of indicatorLabel's elements
    for nodeName in indicatorLabel.components():
        guiObject = indicatorLabel.component(nodeName)
        guiObject["scale"] = scale
        if isinstance(guiObject, OnscreenText):
            guiObject.setPos(0,-half_scale)
        else:
            guiObject.setPos(0,0,0)
